package test.lu.materidesign;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import java.util.ArrayList;
import java.util.Collection;

public class MainActivity extends AppCompatActivity {
    CircleView c;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        c = (CircleView) findViewById(R.id.circle);
        c.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null == v.getAnimation()) {
                    startMove();
                }else{
                    v.clearAnimation();
                }
            }
        });
    }

    private void startMove(){
//        ObjectAnimator animator = ObjectAnimator.ofFloat(c, "translationY", new float[]{0, 500, 1800, 2000});
        PathPoint[] ps = new PathPoint[]{
                PathPoint.moveTo(100, 100),
                PathPoint.lineTo(100, 1800),
                PathPoint.quadTo(200, -300, 600, 1800),
                PathPoint.cubicTo(300, -400, -500, -500, 800, 800)
        };

        ObjectAnimator animator = ObjectAnimator.ofObject(MainActivity.this, "pathPoint", new PathEvaluator(), ps);
        animator.setDuration(600 * 10);
        animator.start();
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                c.setTranslationX(100);
                c.setTranslationY(100);
            }

        });
    }

    public void setPathPoint(PathPoint pp){
        c.setTranslationX(pp.x);
        c.setTranslationY(pp.y);
        c.requestLayout();
    }

    Collection<PathPoint> initPath(){
        ArrayList<PathPoint> list = new ArrayList<>();
        list.add(PathPoint.moveTo(100, 100));
        list.add(PathPoint.lineTo(500, 800));
        list.add(PathPoint.lineTo(800, 800));
        list.add(PathPoint.lineTo(800, 1500));
//        Path path = new Path();
//        path.quadTo();
        return list;
    }

}
